package com.wuxiaofan.service;

import com.wuxiaofan.entity.Order;
import com.wuxiaofan.entity.SeckillOrder;
import com.wuxiaofan.entity.User;
import com.wuxiaofan.mapper.OrderMapper;
import com.wuxiaofan.mapper.SeckillOrderMapper;
import com.wuxiaofan.vo.GoodsVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class OrderService {

    @Resource
    private SeckillOrderMapper seckillOrderMapper;
    @Resource
    private OrderMapper orderMapper;

    public List<SeckillOrder> findByUserIdAndGoodsId(long userId, long goodsId) {
        return seckillOrderMapper.findByUserIdAndGoodsId(userId, goodsId);
    }

    public SeckillOrder seckill(User user, GoodsVO goods) {
        Order order = new Order();
        order.setUserId(user.getId());
        order.setGoodsId(goods.getId());
        order.setDeliveryAddrId(0L);
        order.setGoodsName(goods.getGoodsName());
        order.setStatus("0");
        order.setGoodsPrice(goods.getSeckillPrice());
        order.setPayDate(new Date());
        order.setOrderChannel("H5");
        order.setGoodsCount(1);
        order.setCreateDate(new Date());
        orderMapper.insert(order);

        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setOrderId(order.getId());
        seckillOrder.setUserId(user.getId());
        seckillOrder.setGoodsId(goods.getId());
        seckillOrderMapper.insert(seckillOrder);
        return seckillOrder;
    }
}
